#include "lh_mem.h"

LH_U8 LH_DataBuff[LH_DATA_BUFF_SIZE];               
LH_U8 LH_BufFlag = LH_BUFF_FREE;                    

static LH_S8  LH_S8Unit[LH_S8UNIT_NUM];            									
static LH_S8  LH_S8TextUnit[LH_S8TEXT_NUM*LH_S8TEXT_UNIT_SIZE]; 					
static LH_S8  LH_S8MultiEditUnit[LH_S8_MULTIEDIT_NUM*LH_S8_MULTIEDIT_UNIT_SIZE]; 	
static LH_S16 LH_S16Unit[LH_S16UNIT_NUM];          									
static LH_S16 LH_S16TUnit[2*LH_S16TUNIT_NUM];      									
static LH_S16 LH_GraphUnit[LH_GRAPH_UNIT_NUM*LH_GRAPH_UNIT_SIZE]; 					
static LH_WIDGET LH_WidgetBuff[LH_WIDGET_BUFF_NUM]; 								



void LH_MemInit(void)
{
    LH_U16 Count1;
    
    for(Count1 = 0; Count1 < LH_S8UNIT_NUM; Count1 ++)
        LH_S8Unit[Count1] = 127;
    
    for(Count1 = 0; Count1 < (LH_U32)LH_S8TEXT_NUM*LH_S8TEXT_UNIT_SIZE; Count1 += LH_S8TEXT_UNIT_SIZE)
        LH_S8TextUnit[Count1] = 127;
    
    for(Count1 = 0; Count1 < (LH_U32)LH_S8_MULTIEDIT_NUM*LH_S8_MULTIEDIT_UNIT_SIZE; Count1 += LH_S8_MULTIEDIT_UNIT_SIZE)
        LH_S8MultiEditUnit[Count1] = 127;
    
    for(Count1 = 0; Count1 < LH_S16UNIT_NUM; Count1 ++)
        LH_S16Unit[Count1] = 32767;
    
    for(Count1 = 0; Count1 < LH_S16TUNIT_NUM; Count1 ++)
        LH_S16TUnit[Count1] = 32767;
    
    for(Count1 = 0; Count1 < (LH_U32)LH_GRAPH_UNIT_NUM*LH_GRAPH_UNIT_SIZE; Count1 += LH_GRAPH_UNIT_SIZE)
        LH_GraphUnit[Count1] = 32767;  
    
    for(Count1 = 0; Count1 < LH_WIDGET_BUFF_NUM; Count1 ++)
        LH_WidgetBuff[Count1].WidgetType = LH_WID_NULL;  
}


LH_S8 *GetS8Unit(LH_WIDGET *widget)
{
    LH_U32 Count1 = 0;
    
    
    if ( (widget->WidgetType == LH_LABEL) || (widget->WidgetType == LH_EDIT) )        
    { 
        for(; Count1 < (LH_U32)LH_S8UNIT_NUM*LH_S8TEXT_UNIT_SIZE; Count1 += LH_S8TEXT_UNIT_SIZE)
        {
            if(LH_S8TextUnit[Count1] == 127)
            { 
                LH_S8TextUnit[Count1] = 0;
                return &LH_S8TextUnit[Count1];
            }
        }
    }
    else if (widget->WidgetType == LH_MULTIEDIT)     
    {
        for(; Count1 < (LH_U32)LH_S8_MULTIEDIT_NUM*LH_S8_MULTIEDIT_UNIT_SIZE; Count1 += LH_S8_MULTIEDIT_UNIT_SIZE)
        {
            if(LH_S8MultiEditUnit[Count1] == 127)
            { 
                LH_S8MultiEditUnit[Count1] = 0;
                return &LH_S8MultiEditUnit[Count1];
            }
        }
    }
    else
        for (; Count1 < LH_S8UNIT_NUM; Count1 ++)
            if(LH_S8Unit[Count1] == 127)
            {
                LH_S8Unit[Count1] = 0;
                return &LH_S8Unit[Count1];
            }
    
    return LH_NULL;
}



void FreeS8Unit(LH_WIDGET *widget)
{
    *(LH_S8 *)widget->PData = 127;
}



LH_S16 *GetS16Unit(LH_WIDGET *widget)
{
    LH_U32 Count1 = 0;
    
    if (widget->WidgetType == LH_DIALOG)        
    {
        for(; Count1 < LH_S16TUNIT_NUM; Count1 += 2)
            if(LH_S16TUnit[Count1] == 32767)
            {
                LH_S16TUnit[Count1] = 0;
                return &LH_S16TUnit[Count1];
            }
    }
    else if (widget->WidgetType == LH_GRAPH)    
    {
        for(Count1 = 0; Count1 < (LH_U32)LH_GRAPH_UNIT_NUM*LH_GRAPH_UNIT_SIZE; Count1 += LH_GRAPH_UNIT_SIZE)
            if(LH_GraphUnit[Count1] == 32767)
            {
                LH_GraphUnit[Count1] = 0;  
                return &LH_GraphUnit[Count1];
            }
    }
    else
    {
        for(Count1 = 0; Count1 < LH_S16UNIT_NUM; Count1 ++)
            if(LH_S16Unit[Count1] == 32767)
            {
                LH_S16Unit[Count1] = 0;
                return &LH_S16Unit[Count1];
            }
    }
    
    return LH_NULL;
}



void FreeS16Unit(LH_WIDGET *widget)
{
    *(LH_S16 *)widget->PData = 32767;
}



LH_WIDGET *GetWidBuffUnit(void)
{
    LH_U16 i1 = 0;
    
    for(; i1 < LH_WIDGET_BUFF_NUM; i1 ++)
        if(LH_WidgetBuff[i1].WidgetType == LH_WID_NULL)
        {
            LH_WidgetBuff[i1].WidgetType = LH_WID_OCCUPY;
            return &LH_WidgetBuff[i1];
        }
        
    return LH_NULL; 
}



void FreeWidBuffUnit(LH_WIDGET *widget)
{
    if (widget->WidgetType == LH_DIALOG)
    {
        FreeLHDialogBuff(widget);           
        while(widget != LH_NULL)
        {
            widget->WidgetType = LH_WID_NULL;
            widget = widget->NextWidget;
        }
    }
    else
    {
        LH_WIDGET *LastWid = widget->LastWidget;
        LH_WIDGET *NextWid = widget->NextWidget;
        
        if (NextWid != LH_NULL)
            LastWid->NextWidget = NextWid;
        else
            LastWid->NextWidget = LH_NULL;
        
        widget->WidgetType = LH_WID_NULL;
    }   
}





